package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.xuecheng.api.content.model.dto.TeachplanMediaDTO;
import com.xuecheng.common.domain.code.CommonErrorCode;
import com.xuecheng.common.domain.response.RestResponse;
import com.xuecheng.content.convert.TeachplanMediaConvert;
import com.xuecheng.content.entity.TeachplanMedia;
import com.xuecheng.content.mapper.TeachplanMediaMapper;
import com.xuecheng.content.service.TeachplanMediaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class TeachplanMediaServiceImpl extends ServiceImpl<TeachplanMediaMapper, TeachplanMedia> implements TeachplanMediaService {

    /**
     * 根据媒资id查询课程计划关联信息
     *
     * @param mediaId
     * @return
     */
    public RestResponse<List<TeachplanMediaDTO>> getByMediaId4s(Long mediaId) {
        // 1.判断关键数据
        if (ObjectUtils.isEmpty(mediaId)) {
            return RestResponse.validfail(CommonErrorCode.E_100101);
        }

        // 2.根据mediaid获得数据
        LambdaQueryWrapper<TeachplanMedia> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TeachplanMedia::getMediaId, mediaId);

        List<TeachplanMedia> teachplanMedias = this.list(queryWrapper);

        List<TeachplanMediaDTO> dtos = Collections.emptyList();
        if (!(CollectionUtils.isEmpty(teachplanMedias))) {
            dtos = TeachplanMediaConvert.INSTANCE.entitys2dtos(teachplanMedias);
        }

        RestResponse<List<TeachplanMediaDTO>> success = RestResponse.success(dtos);

        return success;
    }
}
